//
//  CJUserInfoManager.m
//  DatangLife
//
//  Created by 陈建 on 2018/5/25.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJUserInfoManager.h"
#import "CJFileCacheManager.h"

static CJUserInfoManager *_singleton = nil;
@implementation CJUserInfoManager
+ (instancetype)sharedManager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _singleton = [[CJUserInfoManager alloc] init];
    });
    return _singleton;
}

// 当前用户信息
- (CJUserInfoManager *)currentUserInfo {

    id obj = [CJFileCacheManager getObjectByFileName:NSStringFromClass([UserInfoModel class])];
    if (obj != nil) {
        return  obj;
    }
    return nil;
}

// 重置用户信息
- (void)resetUserInfoWithUserInfo:(UserInfoModel *)userInfo {
    [userInfo archive];
}

// 登陆
- (void)didLoginInWithUserInfo:(id)userInfo {
    
    UserInfoModel *userinfo = [UserInfoModel modelWithDictionary:userInfo];
    [userinfo archive];
//
    [CJFileCacheManager saveUserData:@YES forKey:kCJHasLoginFlag];
}

// 退出登陆
- (void)didLoginOut {
    [CJFileCacheManager removeObjectByFileName:NSStringFromClass([CJUserInfoManager class])];
    
    [CJFileCacheManager saveUserData:@NO forKey:kCJHasLoginFlag];
}

// 判断是否是登陆状态
- (BOOL)isLogin {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kCJHasLoginFlag] && [CJUserInfoManager sharedManager].currentUserInfo) {
        return YES;
    }else{
        return NO;
    }
    
}

@end
